<!DOCTYPE html>
<html>

<head>
    <title>WebSocket Example</title>
    <script>
        // 创建WebSocket连接
        var socket = new WebSocket("ws://127.0.0.1:8080/common/ws/1");

        // 当连接建立时触发
        socket.onopen = function () {
            console.log("WebSocket连接已建立");
        };

        // 当接收到消息时触发
        socket.onmessage = function (event) {
            var message = event.data;
            console.log("收到消息: " + message);
        };

        // 当连接关闭时触发
        socket.onclose = function (event) {
            console.log("WebSocket连接已关闭");
        };

        // 发送消息
        function sendMessage() {
            var message = document.getElementById("message").value;
            socket.send(
                JSON.stringify({
                    "to_id": "2",
                    "type": "1",
                    "msg": message
                }));
        }
    </script>
</head>

<body>
    <h1>WebSocket Example</h1>
    <input type="text" id="message" placeholder="输入消息">
    <button onclick="sendMessage()">发送消息</button>
</body>

</html>